package com.china08.yxyapi.api.manage.stu.myclass.moments;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMoments;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsPraiseRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 班級圈--家长--班级圈详情
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class MomentsStuViewDetailsController extends YxyApiBaseController {

    @Autowired
    private YxyMomentsRepository yxyMomentsRepository;

    @Autowired
    private YxyMomentsPraiseRepository yxyMomentsPraiseRepository;

    @RequestMapping(value = "/api_yw/manage/stu/moments/momentsStuViewDetails/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        YxyMoments yxyMomentsList = yxyMomentsRepository.findById(id);
        RespModel respModel = new RespModel();
        respModel.id = yxyMomentsList.getId();
        respModel.avatar = yxyMomentsList.getAvatar();
        respModel.createdDate = DateToolUtils.format(yxyMomentsList.getCreatedDate(), "yyyy-MM-dd HH:mm");
        respModel.senderNick = yxyMomentsList.getSenderNick();
        respModel.senderId = yxyMomentsList.getSenderId();
        respModel.senderUsername = yxyMomentsList.getSenderUsername();
        respModel.classId = yxyMomentsList.getClassId();
        respModel.classNick = yxyMomentsList.getClassNick();
        respModel.styleIntro = yxyMomentsList.getStyleIntro();
        respModel.isShareAll = yxyMomentsList.getIsShareAll();
        respModel.commentCount = yxyMomentsList.getCommentCount();
        respModel.img = yxyMomentsList.getImgs();
        // 是否点赞
        respModel.praise = yxyMomentsPraiseRepository.countByMomentsIdAndUserId(id, userId) > 0;
        respModel.status = yxyMomentsList.getStatus();
        respModel.praiseCount = yxyMomentsList.getPraiseCount();
        respModel.praiseUserNickStr = yxyMomentsList.getPraiseUserNickStr("、");

        // 是否自己的班级圈 （前端以此判断是否显示删除）
        respModel.ownMoments = userId.equals(yxyMomentsList.getSenderId());
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // ID
        public String id;
        // 发布时间
        public String createdDate;
        // 发送者昵称
        public String senderNick;
        // 发送者ID
        public String senderId;
        // 发送人头像
        public String avatar;
        // 发送者用户名
        public String senderUsername;
        // 班级ID
        public String classId;
        // 班级名称h
        public String classNick;
        // 班级圈内容
        public String styleIntro;
        // 是否公开
        public Integer isShareAll;
        // 评论数量
        public Integer commentCount;
        // 发布图片
        public List<String> img;
        // 状态 0:删除 1:有效
        public Integer status;
        // 点赞
        public boolean praise;
        // 点赞数量
        public Integer praiseCount;
        // 点赞人信息
        public String praiseUserNickStr;
        // 是否自己的班级圈
        public boolean ownMoments;
    }
}
